<?php
namespace app\api\service;

use think\facade\Db;
use think\facade\Config;

/**
 * 市级老板表
 * Class CityBossService
 *
 * @package app\admin\service
 */
class CityBossService
{

    /**
     * 当前实例
     *
     * @var object
     */
    protected static $instance;

    /**
     * 表前缀
     *
     * @var string
     */
    protected $tablePrefix;

    /**
     * 表后缀
     *
     * @var string
     */
    protected $tableSuffix;

    /**
     * 表名
     *
     * @var string
     */
    protected $tableName;

    /**
     * 构造方法
     * SystemLogService constructor.
     */
    public function __construct($province_id, $city_id)
    {
        $this->tablePrefix = Config::get('database.connections.mysql.prefix');
        $this->tableSuffix = $province_id . "_" . $city_id;
        $this->tableName = "{$this->tablePrefix}syb_city_boss_{$this->tableSuffix}";
        return $this;
    }

    /**
     * 获取实例对象
     *
     * @return SystemLogService|object
     */
    public static function instance()
    {
        if (is_null(self::$instance)) {
            self::$instance = new static();
        }
        return self::$instance;
    }

    /**
     * 保存数据
     *
     * @param
     *            $data
     * @return bool|string
     */
    public function save($data)
    {
        Db::startTrans();
        try {
            $this->detectTable();
            Db::table($this->tableName)->insert($data);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return $e->getMessage();
        }
        return true;
    }

    /**
     * 保存数据
     *
     * @param
     *            $data
     * @return bool|string
     */
    public function saveAll($data)
    {
        Db::startTrans();
        try {
            $this->detectTable();
            Db::table($this->tableName)->insertAll($data);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return $e->getMessage();
        }
        return true;
    }

    /**
     * 检测数据表
     *
     * @return bool
     */
    protected function detectTable()
    {
        $check = Db::query("show tables like '{$this->tableName}'");
        if (empty($check)) {
            $sql = $this->getCreateSql();
            Db::execute($sql);
        }
        return true;
    }

    public function getAllTableList()
    {}

    /**
     * 根据后缀获取创建表的sql
     *
     * @return string
     */
    protected function getCreateSql()
    {
        return <<<EOT
CREATE TABLE `{$this->tableName}` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `province_id` int(10) unsigned DEFAULT '0' COMMENT '省份ID',
  `city_id` int(10) unsigned DEFAULT '0' COMMENT '城市ID',
  `name` varchar(255) NOT NULL DEFAULT '' COMMENT '企业名称',
  `telphone` varchar(100)  DEFAULT '' COMMENT '联系方式',
  `contacts` varchar(100)  DEFAULT '' COMMENT '联系人名称',
  `registered_capital` varchar(100)  DEFAULT '' COMMENT '企业注册资本',
  `is_hightech` smallint(1) NOT NULL DEFAULT '2' COMMENT '是否高新企业 {radio} (1:是,2:否)',
  `scale` varchar(100)  DEFAULT '' COMMENT '企业成员规模范围',
  `province` varchar(100)  DEFAULT '' COMMENT '省',
  `city` varchar(100) DEFAULT '' COMMENT '市',
  `create_time` int(11) DEFAULT NULL COMMENT '创建时间',
  `update_time` int(11) DEFAULT NULL COMMENT '更新时间',
  `delete_time` int(11) DEFAULT NULL COMMENT '删除时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='城市老板表 - {$this->tableSuffix}';
EOT;
    }
}
